//
//  APPTools.m
//  TableView&CollectionView
//
//  Created by Mihawk on 16/05/15.
//  Copyright © 2016年 米华. All rights reserved.
//

#import "APPTools.h"
//#import "AFNetworking.h"
#import<CommonCrypto/CommonDigest.h>


@implementation APPTools

//+ (void)GETWithURL:(NSString *)urlStr
//               par:(NSDictionary *)dic
//           success:(void (^)(id))responseObj failed:(void (^)(NSError *))err
//{
//    
//    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
//    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSLog(@"%@", documentPath);
//    
//    // 文件管理器
//    NSFileManager *manager = [NSFileManager defaultManager];
//    NSString *musicCaches = [documentPath stringByAppendingPathComponent:@"CachesFile"];
//    BOOL resultFile =  [manager createDirectoryAtPath:musicCaches withIntermediateDirectories:YES attributes:nil error:nil];
//    if (resultFile) {
//        NSLog(@"创建缓存文件夹成功");
//    }
//    // 拼接缓存路径
//    NSString *path = [NSString stringWithFormat:@"%@.plist", [self haxi:urlStr]];
//    NSString *textFilePath = [musicCaches stringByAppendingPathComponent:path];
//    // 检测网络连接的单例,网络变化时回调方法
//    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        NSLog(@"网络状态 = %ld", status);
//    }];
//    
//
//    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
//    // 有的返回的数据格式，AFN不支持解析，所以我们要设置一下。让AFN支持。
//    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
//    
//    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    
//    [man GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        [netWorkManager stopMonitoring];
//        BOOL result = [responseObject writeToFile:textFilePath atomically:YES];
//        if (result) {
//            NSLog(@"写入文件成功");
//        }
//        else {
//            NSLog(@"写入失败");
//        }
//        
//        responseObj(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        id result = [NSMutableDictionary dictionaryWithContentsOfFile:textFilePath];
//        if (result) {
//            NSLog(@"网络加载失败,从本地获取数据");
//        }
//        err(error);
//        responseObj(result);
//
//    }];
//}
//
//+ (void)POSTWithURL:(NSString *)urlStr
//                par:(NSDictionary *)dic
//            success:(void (^)(id))response failed:(void (^)(NSError *))err
//{
//    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
//    
//    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
//    
//    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    
//    NSLog(@"%@", documentPath);
//    
//    //文件管理器
//    NSFileManager *manager = [NSFileManager defaultManager];
//    
//    NSString *foodCaches = [documentPath stringByAppendingPathComponent:@"CachesFile"];
//    
//    BOOL resultFile = [manager createDirectoryAtPath:foodCaches withIntermediateDirectories:YES attributes:nil error:nil];
//    
//    if (resultFile) {
//        NSLog(@"创建缓存文件夹成功");
//    }
//
//    //拼接缓存路径
//    NSString *path = [NSString stringWithFormat:@"%@.plist", [self haxi:dic.description]];
//    
//    NSString *textFilePath = [foodCaches stringByAppendingPathComponent:path];
//    
//    NSLog(@"%@", path);
//    
//    // 检查网络连接单例
//    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        NSLog(@"网络状态 = %ld", status);
//    }];
//    
//
//    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
//    
//    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
//    
//     urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    
//    [man POST:urlStr parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        [netWorkManager stopMonitoring];
//        
////        BOOL result = [responseObject writeToFile:textFilePath atomically:YES];
////        
//        BOOL result = [NSKeyedArchiver archiveRootObject:responseObject  toFile:textFilePath];
//        
//        if (result) {
//            NSLog(@"文件写入成功");
//        }
//        else {
//            NSLog(@"文件写入失败");
//        }
//        
//        
//        response(responseObject);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
////        id result = [NSMutableDictionary dictionaryWithContentsOfFile:textFilePath];
//        
//        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:textFilePath];
//        if (result) {
//            NSLog(@"网络加载失败,从本地获取数据");
//        }
//
//        
//        err(error);
//        response(result);
//
//    }];
//    
// 
//}


+ (NSString *)haxi:(NSString *)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}


//+ (CGSize)labelRectWithSize:(CGSize)size LabelText:(NSString *)labelText Font:(UIFont *)font{
//    
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
//    
//    CGSize actualsize = [labelText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
//    
//    return actualsize;
//    
//}

+ (CGRect)labelRectWithSize:(CGSize)size LabelText:(NSString *)labelText Font:(UIFont *)font {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    
    CGRect rec = [labelText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rec;
}


@end
